package first.endtoend

import org.springframework.dao.DataIntegrityViolationException

import java.text.SimpleDateFormat

class PortfolioDetailController
{

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]


    def index()
    {
        redirect(action: "list", params: params)
    }

    def list(Integer max)
    {
        params.max = Math.min(max ?: 10, 100)
        [portfolioDetailInstanceList: PortfolioDetail.list(params), portfolioDetailInstanceTotal: PortfolioDetail.count()]
    }

    def create()
    {
        def portfolioDetailInstance = new PortfolioDetail(params)
        [portfolioDetailInstance: portfolioDetailInstance, idPdf: params.idPdf]
    }

    def save()
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd") //format date
        Date startDate = sdf.parse(params.startDate) //format date
        Date endDate = sdf.parse(params.endDate) //format date

        def portfolioDetailInstance = new PortfolioDetail(params)
        Portfolio pdf = Portfolio.findById(params.idPdf) // get the portfolio
        portfolioDetailInstance.portfolio = pdf
        Aid aid = new Aid() //Creating a new aid
        aid.startDate = startDate //Creating a new aid
        aid.endDate = endDate //Creating a new aid
        aid.dateCreated = startDate //Creating a new aid
        aid.category = pdf.family.category
        // Setting the category of this aid with the category of the family, that why I took the portfolio, it's for getting the family and then the category...
        // I know it's weird but the initial design it's like that and it takes too much for making changes in the backend and the android app. Sorry pal :/
        aid.valid = true //Creating a new aid
        aid.save(flush: true)
        portfolioDetailInstance.aid = aid
        if (!portfolioDetailInstance.save(flush: true))
        {
            render(view: "create", model: [portfolioDetailInstance: portfolioDetailInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'portfolioDetail.label', default: 'PortfolioDetail'), portfolioDetailInstance.id])
        redirect(controller: "family", action: "show", id: pdf.family.id)
    }

    def show(Long id)
    {
        def portfolioDetailInstance = PortfolioDetail.get(id)
        if (!portfolioDetailInstance)
        {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'portfolioDetail.label', default: 'PortfolioDetail'), id])
            redirect(action: "list")
            return
        }

        [portfolioDetailInstance: portfolioDetailInstance]
    }

    def edit(Long id)
    {
        def portfolioDetailInstance = PortfolioDetail.get(id)
        if (!portfolioDetailInstance)
        {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'portfolioDetail.label', default: 'PortfolioDetail'), id])
            redirect(action: "list")
            return
        }

        [portfolioDetailInstance: portfolioDetailInstance]
    }

    def update(Long id, Long version)
    {
        def portfolioDetailInstance = PortfolioDetail.get(id)

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd") //format date
        Date startDate = sdf.parse(params.startDate) //format date
        Date endDate = sdf.parse(params.endDate) //format date

        if (!portfolioDetailInstance)
        {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'portfolioDetail.label', default: 'PortfolioDetail'), id])
            redirect(action: "list")
            return
        }

        if (version != null)
        {
            if (portfolioDetailInstance.version > version)
            {
                portfolioDetailInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                        [message(code: 'portfolioDetail.label', default: 'PortfolioDetail')] as Object[],
                        "Another user has updated this PortfolioDetail while you were editing")
                render(view: "edit", model: [portfolioDetailInstance: portfolioDetailInstance])
                return
            }
        }
        portfolioDetailInstance.properties = params
        portfolioDetailInstance.aid.startDate = startDate
        portfolioDetailInstance.aid.endDate = endDate

        if (!portfolioDetailInstance.save(flush: true))
        {
            render(view: "edit", model: [portfolioDetailInstance: portfolioDetailInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'portfolioDetail.label', default: 'PortfolioDetail'), portfolioDetailInstance.id])
        redirect(action: "show", id: portfolioDetailInstance.id)
    }

    def delete(Long id)
    {
        def portfolioDetailInstance = PortfolioDetail.get(id)
        if (!portfolioDetailInstance)
        {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'portfolioDetail.label', default: 'PortfolioDetail'), id])
            redirect(action: "list")
            return
        }

        try
        {
            portfolioDetailInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'portfolioDetail.label', default: 'PortfolioDetail'), id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e)
        {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'portfolioDetail.label', default: 'PortfolioDetail'), id])
            redirect(action: "show", id: id)
        }
    }
}
